<?php

namespace app\api\controller\v1\common;


use app\model\plan\PlanStaff;
use app\model\plan\snap\Snap;
use app\Request;
use app\services\face\FaceService;
use think\facade\App;
use think\Image;

class FaceController
{
    public function check(Request $request)
    {

        $file = $request->file('file');
        $coursewareId = $request->post('courseware_id' , 0);
        $planId = $request->post('plan_id' , 0);
        $type = $request->post('type' , 0);

//        validate(['file' => [
//            'fileSize' => 500 * 1024 * 1024,
//            'fileExt'  => 'png,jpg,jpeg,gif,xls,xlsx,mp4'
//        ]])->check(['file' => $file]) ;

        $saveName = \think\facade\Filesystem::disk('public')->putFile( 'face', $file);
        $saveName = 'upload/' . $saveName ;
        $sign = Image::open( $saveName ) ;
        $sign->thumb(230 , 300) ;
        $sign->save(App::getRootPath().'public/' . $saveName) ;
        $uploadUrl = $request->domain() .'/' . $saveName ;

        $user = $request->staffInfo();
        $face = new FaceService() ;


        $ret = $face->faceverify($user['img_url'] , $uploadUrl) ;
        if($type == 1){
            Snap::logAdd($request->staffId() , $coursewareId , $type ,$uploadUrl , $ret , $planId) ;
        }
        if(!$ret){
            return app('json')->fail('身份验证失败，活体检测未通过');
        }
        return app('json')->success('审核成功' , [
            'url' => $uploadUrl
        ]);
    }
}